<?php

namespace app\api\services\user;

use app\api\constant\StatusConst;
use app\api\model\UserAddress;
use app\api\services\BaseServices;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\HttpException;
use think\Paginator;

class AddressServices extends BaseServices
{
    /**
     * @param int $userid
     * @param array $addressInfo
     * @return true
     * @throws Exception
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function editAddress(int $userid, array $addressInfo): bool
    {
        $addressInfo['province'] = $addressInfo['address']['province'];
        $addressInfo['city'] = $addressInfo['address']['city'];
        $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
        $addressInfo['district'] = $addressInfo['address']['district'];
        $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? StatusConst::NORMAL : StatusConst::FORBIDDEN;
        $addressInfo['user_id'] = $userid;

        $addressCheck = [];
        if ($addressInfo['id']) {
            $addressCheck = UserAddress::getOne((int)$addressInfo['id']);
        }
        if ($addressCheck && $addressCheck['is_del'] == StatusConst::FORBIDDEN && $addressCheck['user_id'] == $userid) {
            $id = (int)$addressInfo['id'];
            unset($addressInfo['id']);
            if (!UserAddress::update($addressInfo, ['id' => $id], true)) {
                throw new Exception("更新失败");
            }

            return true;
        } else {
            $addrCount = UserAddress::where('user_id', '=', $userid)->count();
            if (!$addrCount) {
                $addressInfo['is_default'] = StatusConst::NORMAL;
            }
            try {
                UserAddress::create($addressInfo, true);
            } catch (Exception $e) {
                throw new Exception("新增地址失败");
            }
            return true;
        }
    }

    /**
     * 获取用户地址列表
     * @param int $userid
     * @param string $field
     * @return Paginator
     * @throws DbException
     */
    public function getUserAddressList(int $userid, string $field = ''): Paginator
    {
        [$page, $limit] = self::getPageValue();
        $where = [];
        $where['user_id'] = $userid;
        $where['is_del'] = StatusConst::NORMAL;
        return UserAddress::where($where)->field($field)->order('id', 'desc')->paginate([
            'list_rows' => $limit,
            'page' => $page
        ]);
    }

    public function getByIdOrDefaultAddress($userid, $id = null)
    {
        if ($id) {
            $address = UserAddress::where('user_id', $userid)->where('id', $id)->find();
            if (empty($address)) {
                throw new Exception("地址不存在");
            }
        } else {
            $address = UserAddress::where('user_id', $userid)->where('is_default', StatusConst::NORMAL)->find() ?? "";
        }
        return $address;
    }

}